<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" style="padding: 20px;" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="开具日期" prop="createDate">
        <el-date-picker clearable
          v-model="queryParams.createDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择开具日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="病历号" prop="medicalRecordId">
        <el-input
          v-model="queryParams.medicalRecordId"
          placeholder="请输入病历号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 处方信息显示表格 -->
    <el-table v-loading="loading" :data="prescriptionList">
      <el-table-column label="开具日期" align="center" prop="createDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付数" align="center" prop="count" />
      <el-table-column label="病历号" align="center" prop="medicalRecordId" />
      <el-table-column label="总价" align="center" prop="amount" />
      <el-table-column label="疗法" align="center" prop="therapy" />
      <el-table-column label="疗法细节" align="center" prop="therapyDetails" />
      <el-table-column label="医疗建议" align="center" prop="medicalAdvice" />
      <el-table-column label="药物使用频率" align="center" prop="frequency" />
      <el-table-column label="使用方法" align="center" prop="usageMeans" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button style="width: 80px;" type="primary" @click="listDrugsOfPrescription(scope.row)">查看药品</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page.sync="queryParams.pageNum"
      v-model:limit.sync="queryParams.pageSize"
      @pagination="getList"
      style="margin-bottom: 50px;"
    />

    <!-- 所含药品对话框 -->
    <el-dialog title="所含药品" v-model="open" append-to-body>
      <!-- 药品信息表格 -->
      <el-table v-loading="loading" :data="drugList">
        <el-table-column label="药品编号" align="center" prop="code" />
        <el-table-column label="名称" align="center" prop="name" />
        <el-table-column label="通用名称" align="center" prop="genericName" />
        <el-table-column label="规格" align="center" prop="specification" />
        <el-table-column label="制造商" align="center" prop="manufacturer" />
        <el-table-column label="批准文号" align="center" prop="approvalNumber" />
        <el-table-column label="存储条件" align="center" prop="storageConditions" />
        <el-table-column label="适应症" align="center" prop="disease" />
        <el-table-column label="单价" align="center" prop="price" />
        <el-table-column label="剂型" align="center" prop="dosageForm" />
        <el-table-column label="分类" align="center" prop="type" />
        <el-table-column label="成分" align="center" prop="ingredients" />
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
import { listPrescription, getPrescription, delPrescription, addPrescription, updatePrescription } from "@/api/emr/prescription";

import { getDrugsByPreId } from "@/api/emr/prescription"

export default {
  name: "Prescription",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 处方表格数据
      prescriptionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        createDate: null,
        count: null,
        medicalRecordId: null,
        amount: null,
        therapy: null,
        therapyDetails: null,
        medicalAdvice: null,
        frequency: null,
        usageMeans: null,
        type: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      /** 处方所含药物信息 */
      drugList:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询处方列表 */
    getList() {
      this.loading = true;
      listPrescription(this.queryParams).then(response => {
        this.prescriptionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        status: null,
        createDate: null,
        count: null,
        medicalRecordId: null,
        amount: null,
        therapy: null,
        therapyDetails: null,
        medicalAdvice: null,
        frequency: null,
        usageMeans: null,
        type: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查看药品操作 */
    listDrugsOfPrescription(row){
      console.log(row.id);
      let formPreId = {
        "preId": row.id
      }
      getDrugsByPreId(formPreId).then(response => {
        this.open = true;
        this.drugList = response.data;
      });
    }
  }
};
</script>
